from django.db import models
from uuid import uuid4
from datetime import datetime

'''
重命名上传的图片,避免重名
'''


def image_rename(instance, filename):
    ext = filename.split('.')[-1]
    filename = '{}.{}'.format(uuid4().hex, ext)
    year = datetime.now().year
    month = datetime.now().month
    day = datetime.now().day
    return "newsphoto/{0}/{1}/{2}/{3}".format(year, month, day, filename)


'''
文章数据模型
'''


class Article(models.Model):
    title = models.CharField("标题", max_length=128)
    content = models.TextField("内容")
    user_id = models.IntegerField("用户ID", default=0)
    author = models.CharField("作者", max_length=64)
    c_id = models.IntegerField("类别ID", default=0)
    publish_date = models.DateTimeField("发布时间", auto_now_add=True, editable=True)
    update_date = models.DateTimeField("更新时间", auto_now=True, null=True)
    image = models.ImageField("图片", upload_to=image_rename, blank=True)

    class Meta:
        ordering = ['publish_date']


'''
评论的数据模型
'''


class Comments(models.Model):
    article_id = models.IntegerField()
    comment_author_id = models.IntegerField()
    comment_author = models.CharField(max_length=64)
    content = models.TextField()
    comment_date = models.DateTimeField(null=True)
    parent_id = models.IntegerField()


'''
文章类别信息
'''


class Category(models.Model):
    c_name = models.CharField("类别名称", max_length=16, default="未分类")